"""
time：2024.12.24
进入逍遥模拟器，终端链接设备
对 “学车不” 软件进行登录操作
"""
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
# 定义一个空字典
des_cap = {}

# 告诉appium服务器，你要链接的模拟器的平台是安卓
des_cap['platformName'] = 'android'
# 安卓的版本9
des_cap['platformVersion'] = '9'
# 链接设备的名称
des_cap['deviceName'] = '127.0.0.1:21503'
# 要打开的APP的包名
des_cap['appPackage'] = 'com.android.settings'
# app的界面名
des_cap['appActivity'] = '.Settings'
# 设置字符集为unicode 解决中文无法输入
des_cap['unicodeKeyboard'] = True
# 重置键盘
des_cap['resetKeyboard'] = True

# 初始化对象
driver = webdriver.Remote('http://localhost:4723/wd/hub', des_cap)

print('打开应用')
driver.start_activity('com.bjcsxq.chat.carfriend',
                      '.module_main.activity.MainActivity')
time.sleep(2)
# 定位我的,点击
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/tv_home_mine').click()
time.sleep(2)
# 定位登录,点击
login = driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/mine_username_tv').text
if login == '登录/注册':
    driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/mine_username_tv').click()
    time.sleep(2)
    # 定位输入手机号,
    # driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/login_phone_et').send_keys('17732864463')
    # XPATH定位手机号
    driver.find_element_by_xpath("//*[@text='请输入手机号']").send_keys('17732864463')

    # 定位输入密码
    driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/login_pwd_et').send_keys('a1234567')

    time.sleep(2)

    # 点击登录按钮
    driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/login_btn').click()
    # 点击提示框确认按钮
    driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/btn_neg').click()

print('已经登录')

# 点击报名
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/tv_home_baoming').click()

# 点击北方驾校
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/list_item_school').click()

# 滑动屏幕
# 获取屏幕的宽高
w = driver.get_window_size()['width']
h = driver.get_window_size()['height']
driver.swipe(start_x=w/2, end_x=w/2, start_y=h*0.9, end_y=h*0.1)

# 点击发表
# driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/tv_enrol_postchoolfen').click()
# 调用手势
action = TouchAction(driver)
# 调用手势方法 点击坐标 释放 执行
action.press(x=400, y=1000).release().perform()

# 五星
# driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/star_evaluate_video').click()
time.sleep(2)
action.press(x=400, y=200).release().perform()
# 评论
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/evaluat_comment_video').send_keys('好啊')
# 发表
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/title_other').click()
# 返回
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/title_back').click()

# 评价教练
# 点击教练
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/tv_rl_tab_enrol_personnel').click()

# 点击教练图片
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/img_enrol_items').click()

# 滑动屏幕
# 获取屏幕的宽高
w = driver.get_window_size()['width']
h = driver.get_window_size()['height']
driver.swipe(start_x=w/2, end_x=w/2, start_y=h*0.9, end_y=h*0.1)

# 点击发表
# 调用手势
action = TouchAction(driver)
# 调用手势方法 点击坐标 释放 执行
action.press(x=400, y=1200).release().perform()

# 五星
# driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/star_evaluate_video').click()
time.sleep(2)
action.press(x=400, y=200).release().perform()
# 评论
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/evaluat_comment_video').send_keys('很好啊hhh')
# 发表
driver.find_element_by_id('com.bjcsxq.chat.carfriend:id/title_other').click()

# assert